package com.intellagric.controller.module.agriecom_produce_param;

import com.intellagric.agriecom.module.agriecom_produce_param.ParamService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.AgriecomProduceParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;


@Controller
@RequestMapping("/param")
public class ProduceParamController {
    @Autowired
    private ParamService service;


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/getById")
    @ResponseBody
    public AgriecomProduceParam findParamById(String id){
        return service.selectParamById(id);
    }

    /**
     * 添加参数名
     * @param produceParam
     * @return
     */
    @RequestMapping("/addParam")
    @ResponseBody
    public ResponseMessage addParam(AgriecomProduceParam produceParam){
        String id=UUID.randomUUID().toString().replaceAll("-","");
        if(produceParam!=null){
            produceParam.setId(id);
            service.addParam(produceParam);
        }
        return ResponseMessage.fail();
    }
    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public LayuiDataGridResult delete(String id) {
        return service.deleteParamById(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value="deleteAll",method = RequestMethod.GET)
    @ResponseBody
    public LayuiDataGridResult deleteAll(@RequestParam(name = "ids[]") String[] ids) {
        return service.batchDelete(ids);
    }

    /**
     * 修改信息
     * @param produceParam
     * @return
     */
    @RequestMapping("edit")
    @ResponseBody
    public ResponseMessage edit(AgriecomProduceParam produceParam){
        if(produceParam!=null)
            return service.updateParamByVo(produceParam);
        return ResponseMessage.fail();
    }

    /**
     * 分页列表
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public LayuiDataGridResult query(@RequestParam(value="page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int limit){
        return service.getParamList(page,limit);
    }

    /**
     * 获取分类分页
     * @param page
     * @param limit
     * @param categoryId
     * @return
     */
    @RequestMapping("getListByParam")
    @ResponseBody
    public LayuiDataGridResult getListByParam(@RequestParam(value="page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int limit,String categoryId){
        return service.getParamListByCategory(categoryId,page,limit);
    }

    /**
     * 获得关联结果集
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("getRelationList")
    @ResponseBody
    public LayuiDataGridResult getListByParam(@RequestParam(value="page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "2") int limit){
        return service.getRelationParamList(page,limit);
    }


    @RequestMapping("getListByKeyword")
    @ResponseBody
    public LayuiDataGridResult getListByKeyword(@RequestParam(value="page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int limit,String keyword){
        return service.getListByKeyword(page,limit,keyword);
    }


}
